{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f1ef5c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install biopython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "006129cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# RNA seq\n",
    "from Bio import pairwise2\n",
    "from Bio.pairwise2 import format_alignment\n",
    "\n",
    "def global_alignment(rna1, rna2):\n",
    "    \"\"\" Perform global alignment between two RNA sequences and print the results. \"\"\"\n",
    "    # Perform global alignment using Bio.pairwise2\n",
    "    alignments = pairwise2.align.globalxx(rna1, rna2)\n",
    "    \n",
    "    # Print alignment results\n",
    "    for a in alignments:\n",
    "        print(format_alignment(*a))\n",
    "\n",
    "def local_alignment(rna1, rna2):\n",
    "    \"\"\" Perform local alignment between two RNA sequences and print the results. \"\"\"\n",
    "    # Perform local alignment using Bio.pairwise2\n",
    "    alignments = pairwise2.align.localxx(rna1, rna2)\n",
    "    \n",
    "    # Print alignment results\n",
    "    for a in alignments:\n",
    "        print(format_alignment(*a))\n",
    "\n",
    "def main():\n",
    "    # Example RNA sequences\n",
    "    rna1 = \"AUGCUUCAG\"\n",
    "    rna2 = \"AUGCUUCC\"\n",
    "\n",
    "    # Call the global alignment function\n",
    "    print(\"Global Alignment:\")\n",
    "    global_alignment(rna1, rna2)\n",
    "\n",
    "    # Call the local alignment function\n",
    "    print(\"\\nLocal Alignment:\")\n",
    "    local_alignment(rna1, rna2)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4184442",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GIP\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "def compute_gip_kernel(association_matrix):\n",
    "    def calculate_normalization_constant(matrix):\n",
    "        \"\"\" Calculate the normalization constant 'r' for the Gaussian Kernel based on association data. \"\"\"\n",
    "        squared_norms = np.sum(np.square(np.linalg.norm(matrix, axis=1)))\n",
    "        r = squared_norms / matrix.shape[0]\n",
    "        return r\n",
    "    \n",
    "    num_entities = association_matrix.shape[0]\n",
    "    kernel_matrix = np.zeros((num_entities, num_entities))\n",
    "    r = calculate_normalization_constant(association_matrix)\n",
    "    \n",
    "    for i in range(num_entities):\n",
    "        for j in range(num_entities):\n",
    "            squared_distance = np.square(np.linalg.norm(association_matrix[i, :] - association_matrix[j, :]))\n",
    "            if r == 0:\n",
    "                kernel_matrix[i, j] = 0\n",
    "            elif i == j:\n",
    "                kernel_matrix[i, j] = 1\n",
    "            else:\n",
    "                kernel_matrix[i, j] = np.exp(-squared_distance / r)\n",
    "    \n",
    "    return kernel_matrix\n",
    "\n",
    "def save_matrix(matrix, filename):\n",
    "    \"\"\" Save the given matrix to a CSV file, ensuring the directory exists. \"\"\"\n",
    "    directory = os.path.dirname(filename)\n",
    "    if not os.path.exists(directory):\n",
    "        os.makedirs(directory)\n",
    "    pd.DataFrame(matrix).to_csv(filename, header=None, index=None)\n",
    "\n",
    "def main():\n",
    "    # Load association data from a CSV file into a NumPy array\n",
    "    disease_rna_association = np.array(pd.read_csv('./dataset/ass_del.csv', header=None))\n",
    "\n",
    "    # Calculate GIP kernels for disease and RNA\n",
    "    gip_disease_sim = compute_gip_kernel(disease_rna_association)\n",
    "    gip_rna_sim = compute_gip_kernel(disease_rna_association.T)\n",
    "\n",
    "    # Save the computed GIP kernel matrices ensuring the file exists\n",
    "    save_matrix(gip_disease_sim, './dataset/disease_gip_kernel.csv')\n",
    "    save_matrix(gip_rna_sim, './dataset/rna_gip_kernel.csv')\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6380c489",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"MISIM v2.0 Overview\n",
    "MISIM v2.0 is a tool hosted on http://www.lirmed.com/misim/onevsall, designed to compute microRNA functional similarity. It leverages the HMDD v3.0 dataset to infer similarities based on microRNA-disease associations.\n",
    "\n",
    "Usage Guide\n",
    "Input Data: Submit your microRNA data into the tool's input field.\n",
    "Analysis: MISIM v2.0 calculates similarity scores that aid in predicting new miRNA-disease associations.\n",
    "Visualization: The tool provides network visualization and functional analysis of the microRNAs analyzed.\n",
    "Access: This tool is available for academic use; commercial users need to contact the administrators.\n",
    "This guide should help you get started with using MISIM v2.0 in your research.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b8600f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "library(DOSE)\n",
    "\n",
    "# Function to calculate semantic similarity between diseases\n",
    "calculate_disease_similarity <- function(disease_ids) {\n",
    "  # Ensure the DOSE package is loaded\n",
    "  if (!(\"DOSE\" %in% rownames(installed.packages()))) {\n",
    "    BiocManager::install(\"DOSE\")\n",
    "    library(DOSE)\n",
    "  }\n",
    "  \n",
    "  # Add \"DOID:\" prefix if not already present\n",
    "  disease_ids <- ifelse(grepl(\"^DOID:\", disease_ids), disease_ids, paste0(\"DOID:\", disease_ids))\n",
    "  \n",
    "  # Calculate semantic similarity\n",
    "  similarity_matrix <- doSim(disease_ids, disease_ids, measure = \"Wang\")\n",
    "  \n",
    "  return(similarity_matrix)\n",
    "}\n",
    "\n",
    "# Example usage with multiple disease IDs\n",
    "disease_ids <- c(\"0014667\", \"0050156\", \"0080315\", \"14330\")\n",
    "similarity_matrix <- calculate_disease_similarity(disease_ids)\n",
    "print(similarity_matrix)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6d38e84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Batch download from NCBI could be planned using \"https://www.ncbi.nlm.nih.gov/sites/batchentrez\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
